﻿using Newtonsoft.Json;
using RentHouse.Admin.App_Start;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RentHouse.Admin.Controllers
{
    public class PermissionsController : Controller
    {
        IPermissionsService permissions;
        public PermissionsController(IPermissionsService permissions)
        {
            this.permissions = permissions;
        }

        //返回全部信息
        public string GetAll(int page, int limit)
        {
            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            //字典类（用于存储数据）
            Dictionary<string, object> map = new Dictionary<string, object>();

            List<PermissionsDTO> list = permissions.GetList();

            map.Add("code", "0");//数据格式或类型
            map.Add("msg", "用户列表信息");//数据声明
            map.Add("count", list.Count);//数据总数（用于分页）

            map.Add("data", list
                .OrderBy(u => u.id)//分页前必须根据某一列进行排序，保证数据顺序唯一
                .Skip((page - 1) * limit)//执行当前页起始数据
                .Take(limit));//每页条数

            return JsonConvert.SerializeObject(map);

        }

        /// <summary>
        /// 获取当前用户的权限,带身份验证
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [MyAuthorizationFilter]
        public ActionResult GetPermis(string token)
        {
            List<PermissionsDTO> list = permissions.GetListAdminUserById(int.Parse(token));

            return Json(Suanfa(list, 0), JsonRequestBehavior.AllowGet);
        }

        
        /// <summary>
        /// 用于递归权限算法
        /// </summary>
        /// <param name="list">完整的权限列表信息</param>
        /// <param name="fid">菜单id（就是没有父级的菜单）</param>
        /// <returns>返回的权限列表信息是上下级具有关系，复合左树形结构</returns>
        public List<PermissionsDTO> Suanfa(List<PermissionsDTO> list, int fid)
        {
            List<PermissionsDTO> List2 = list.Where(t => t.fid == fid).ToList();

            foreach (PermissionsDTO item in List2)
            {
                if (item.fid == 0)
                {

                    item.children = Suanfa(list, (int)item.id);

                }

            }

            return List2;
        }




        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Create(PermissionsDTO permissionsDTO)
        {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return permissions.Create(permissionsDTO);

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Delete(PermissionsDTO permissionsDTO)
        {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return permissions.Delete(permissionsDTO);
        }

        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="adminUserDTO"></param>
        /// <returns></returns>
        public string Update(PermissionsDTO permissionsDTO)
        {

            //Response.Headers.Add("Access-Control-Allow-Origin", "*");

            return permissions.Update(permissionsDTO);
        }



    }
}